package com.easyfun.healthmagicbox.comm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;

import com.easyfun.healthmagicbox.HMBMainActivity;
import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.comm.json.PersonProfilesListJsonWrapper;
import com.easyfun.healthmagicbox.comm.json.UserToPersonsListJsonWrapper;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.ormlite.ServerResponseCode;
import com.easyfun.healthmagicbox.pojo.ConstantData;
import com.easyfun.healthmagicbox.pojo.PersonProfile;
import com.easyfun.healthmagicbox.pojo.UserToPersons;
import com.easyfun.healthmagicbox.sync.CommCallback;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;

public class PersonProfileDownloadTask extends BaseCommTask implements Runnable,DatabaseAccess 
{
	
	String personID;

	public PersonProfileDownloadTask(String personID, Context context,
			CommCallback callback) {

		super(context,callback);
		this.personID = personID;
	}
	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(mcontext);
	}
	
	@Override
	public void run() {
		JSONObject serializedObj = new JSONObject();
		String result = "";
		try {
			serializedObj.put("personID", personID);

			Map<String, String> parmas = new HashMap<String, String>();
			parmas.put("jsonstring", serializedObj.toString());
			result = super.uploadByHTTP(parmas, ConstantData.PersonProfileDownloadURI);
			if (ServerResponseCode.parseOf(result) == ServerResponseCode.FAILED) {

				ccb.setCommResult(this, result,
						ServerResponseCode.FAILED);
				return;
			}

			if ((result.length() > 0)) {
				if (ServerResponseCode.parseOf(result) == ServerResponseCode.SUCCESS) {
					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				} else {
					Dao<PersonProfile, Integer> ppdao = getHelper(mcontext)
							.getPersonProfileDao();
					List<PersonProfile> datas =  
							PersonProfilesListJsonWrapper
							.getInstance(result,PersonProfilesListJsonWrapper.class).getMpps();

					
					for (PersonProfile data:datas)
					{
						List<PersonProfile> oldpp = ppdao.queryBuilder().where().eq("personID", data.getPersonID())
							.query();
						if (oldpp.size()==0)
						{
							ppdao.create(data);
						}else if (oldpp.size()==1)
						{
							data.setId(oldpp.get(0).getId());
							ppdao.createOrUpdate(data);							
						}else
						{
							for (PersonProfile old:oldpp)
								ppdao.deleteById(old.getId());
							ppdao.create(data);							
						}
					}
					//if head image changed refresh it.
					HMBMainActivity.refreshImage();
					ccb.setCommResult(this, result,
							ServerResponseCode.SUCCESS);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ccb.setCommResult(this, result,
					ServerResponseCode.FAILED);
		}
	}
	
}
